#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

int g_nums = 100;
pthread_mutex_t g_lock; 

void* my_start(void* arg)
{
    pthread_mutex_lock(&g_lock);
    while(g_nums){
        printf("i am %p, nums is %d\n", pthread_self(), g_nums);
        g_nums--;
    }
    return NULL;
}

int main()
{
    //初始化互斥锁
    pthread_mutex_init(&g_lock , NULL);
    //创建两个线程
    pthread_t tid[2];

    int i;
    for(i = 0; i < 2; ++i){
        int p = pthread_create(&tid[i], NULL, my_start, NULL);
        if(p < 0){
            perror("pthread_create\n");
            return 0;
        }
    }

    //getchar();
    //主线程进行等待
    for(i = 0; i < 2; ++i){
        pthread_join(tid[i], NULL);
    }

    return 0;
}
